package cn.huihuo.weibo.module;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;

/**
 * @author Dan
 * Created by zhengjunbo on 2015/10/31.
 */
public class ParticipationModel extends Model<ParticipationModel> {
    public static final ParticipationModel dao= new ParticipationModel();

    public Page<ParticipationModel> findParticipationByUserId(Integer pageNum,Integer count,String userId){
        return paginate(pageNum,count,"select pp.id pid,p.id prodId,pic.inner_pic_path,pic.out_pic_path,p.prod_title title,pp.n_number ,pp.is_open  "," from participation pp,product p,productpic pic where pp.prod_id=p.id and p.defult_pic_id=pic.id and pp.user_id=? order by  pp.is_open desc, pp.n_number desc,  pp.insert_time desc  ",userId);
    }

    public ParticipationModel findHaveParticipationModel(String userId,Integer productId){
        return findFirst("select * from participation t where t.user_id=? and t.prod_id=?",userId,productId);
    }


    public void  updateByPrductId(Integer productId){
        Db.update("update participation t set t.n_number = t.n_number + 1 where t.prod_id=? and t.is_open=1",productId);
    }

    public void updateZero(Integer partId){
        Db.update("update participation t set t.n_number = 0 where t.id=? ",partId);
    }

    public ParticipationModel findByUserId(String userId){
        return findFirst("select sum(t.n_number) size from  participation t where t.user_id=? and t.is_open=1",userId);
    }
}
